<template>
  <el-dialog title="关联角色" :visible.sync="value" :before-close="cancel">
    <el-checkbox-group v-model="checkRoleIds">
      <el-checkbox :label="item.id+''" v-for="item in roles" :key="item.id">{{item.rname}}</el-checkbox>
    </el-checkbox-group>
    <div slot="footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { list } from "@/api/role";
import { relativeRoles, bindingRoles } from "@/api/user";
export default {
  name: "relativeIndex",
  components: {},
  props: {
    value: Boolean
  },
  data() {
    return {
      userId: null,
      defaultProps: {
        children: "children",
        label: "name"
      },
      checkRoleIds: [],
      roles: []
    };
  },
  methods: {
    async init(userId) {
      this.userId = userId;
      const resp = await relativeRoles(userId);
      this.checkRoleIds = resp.data.map(item => item + "");
      const { data } = await list(this.searchForm);
      this.roles = data;
    },
    async submit() {
      await bindingRoles(this.userId, this.checkRoleIds);
      this.cancel();
    },
    cancel() {
      this.$emit("input", false);
    }
  }
};
</script>

<style lang="scss" scoped>
</style>